package com.woldier.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woldier.common.R;
import com.woldier.entity.Category;
import com.woldier.entity.Dish;
import com.woldier.entity.Setmeal;
import com.woldier.service.CategoryService;
import com.woldier.service.DishService;
import com.woldier.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    DishService dishService;

    @Autowired
    SetmealService setmealService;

    /**
     * 新增菜品分类
     * @param category
     * @return
     */
    @PostMapping
    public R addCategory(@RequestBody Category category){

        categoryService.save(category);

        return R.success("成功");
    }

    /**
     * 获取分页数据
     * 请求网址: http://localhost/category/page?page=1&pageSize=10
     * 请求方法: GET
     * @return
     */

    @GetMapping("page")
    public R<Page> getPage(int page,int pageSize){
        /*初始化分页*/
        Page<Category> categoryPage = new Page<>();

        /*找寻构造器*/
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        /*添加条件*/
        lambdaQueryWrapper.orderByAsc(Category::getSort);

        /*查询*/
        categoryService.page(categoryPage,lambdaQueryWrapper);

        return R.success(categoryPage);
    }

    @DeleteMapping
    public R delete(Long ids){

        /*如果当前菜品关联了菜品不允许删除*/
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,ids);
        List<Dish> list = dishService.list(lambdaQueryWrapper);

        /*如果查询到数据不允许删除*/
        if(list.size() != 0 )
            return R.error("有关联的菜品存在");
        /*如果当前套餐关联了菜品不允许删除*/
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Setmeal> eq = lambdaQueryWrapper1.eq(Setmeal::getCategoryId, ids);

        List<Setmeal> list1 = setmealService.list(lambdaQueryWrapper1);

        /*如果查询到数据不允许删除*/
        if(list1.size() !=0 )
            return R.error("有关联的套餐存在");


        /**/
        categoryService.removeById(ids);


        return R.success("成功");
    }


    /**
     * 分类的修改
     *
     */
    @PutMapping
    public R update(@RequestBody Category category){

        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Category::getId,category.getId());

        categoryService.update(category,lambdaQueryWrapper);

        return R.success("插入成功");

    }


    /**
     * 更具type 查询菜品或套餐
     * @param category
     * @return
     */

    @GetMapping("list")
    public R getByType(Category category){
        /*条件构造器*/
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        /*添加条件 按type查询*/
        lambdaQueryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        /*设置排序方式*/
        /*sort升序updateTime降序*/
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(lambdaQueryWrapper);
        return R.success(list);


    }





}
